package com.yunhe.abnormal.service.base.impl;

import com.yunhe.common.constant.CacheConsts;
import com.yunhe.common.model.data.AbnormalRuleFormulaObject;
import com.yunhe.abnormal.repository.base.AbnormalRuleFormulaObjectRepository;
import com.yunhe.abnormal.service.base.AbnormalRuleFormulaObjectService;
import net.oschina.j2cache.cache.support.util.SpringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @title: AbnormalRuleFormulaObjectServiceImpl
 * @Author Chen Lun
 * @Date: 2021/1/29
 */
@Service
public class AbnormalRuleFormulaObjectServiceImpl implements AbnormalRuleFormulaObjectService {

    @Autowired
    private AbnormalRuleFormulaObjectRepository abnormalRuleFormulaObjectRepository;

    private static final Logger logger = LoggerFactory.getLogger(AbnormalRuleFormulaObjectServiceImpl.class);

    @Override
    @Cacheable(value = CacheConsts.Region.ALARM_RULE,
            key = "{T(com.yunhe.abnormal.consts.AlarmKey).getPidListMapKey()}")
    public Map<String, List<AbnormalRuleFormulaObject>> findAllFormulaObject() {
        Map<String, List<AbnormalRuleFormulaObject>> result = new HashMap<>();
        List<AbnormalRuleFormulaObject> formulaObjectList = abnormalRuleFormulaObjectRepository.findAllAbnormalRuleFormulaObject();
        for (AbnormalRuleFormulaObject formulaObject : formulaObjectList) {
            List<Long> measPointNumbers = formulaObject.getMeasPointNumbers();
            for (Long measPointNumber : measPointNumbers) {
                if (measPointNumber == null) {
                    continue;
                }
                String measPointNumberStr = String.valueOf(measPointNumber);
                List<AbnormalRuleFormulaObject> relatedFormulaObject = result.computeIfAbsent(measPointNumberStr, k -> new ArrayList<>());
                relatedFormulaObject.add(formulaObject);
            }
        }
        return result;
    }

    @Override
    public List<AbnormalRuleFormulaObject> getRelatedFormulaObjectByPointNumber(Long pointNumber) {
        Map<String, List<AbnormalRuleFormulaObject>> formulaObjectMap = SpringUtil.getBean(AbnormalRuleFormulaObjectService.class).findAllFormulaObject();
        return formulaObjectMap == null ? null : formulaObjectMap.get(String.valueOf(pointNumber));
    }

    @Override
    @Cacheable(value = CacheConsts.Region.ALARM_RULE,
            key = "{T(com.yunhe.abnormal.consts.AlarmKey).getPidListKey()}")
    public List<Long> findAllFormulaMeasPointNumber() {
        return abnormalRuleFormulaObjectRepository.getAllMeasPointNumber();
    }

    @Override
    public AbnormalRuleFormulaObject findByRuleAndLevel(Long ruleId, Long levelId) {
        return abnormalRuleFormulaObjectRepository.findByRuleIdAndLevelId(ruleId, levelId);
    }

    @Override
    @CacheEvict(value = CacheConsts.Region.ALARM_RULE, key = "{T(com.yunhe.abnormal.consts.AlarmKey).getPidListMapKey()}")
    public void clearFormulaObjectRedis() {
    }

    @Override
    public List<Long> getMeasPointNumbersByRuleId(Long ruleId) {
        return abnormalRuleFormulaObjectRepository.getMeasPointNumbersByRuleId(ruleId);
    }
}
